<template>
  <div class="setting-body">
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <div class="custom-tips">
        基础设置
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item
            label="显示行数"
            prop="rows"
            :rules="[{ required: true, message: '最大显示行数', trigger: 'blur' }]"
          >
            <el-input-number v-model="form.rows" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="每行显示个数"
            prop="size"
            :rules="[{ required: true, message: '每行显示个数', trigger: 'blur' }]"
          >
            <el-input-number v-model="form.size" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="custom-tips">
        统计项设置
        <div class="button-add">
          <el-button type="text" icon="el-icon-plus" @click="doAdd">新增</el-button>
        </div>
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <draggable v-model="form.data" animation="300" @end="clone">
          <el-col v-for="(item,index) in form.data" :key="'data-'+index" style="border-bottom: 1px solid #e2e2e2;margin-bottom: 10px" :span="24">
            <el-row :gutter="10">
              <el-col :span="24">
                <el-form-item
                  label="显示标题"
                  :prop="'data.'+index+'.label'"
                  :rules="[{ required: true, message: '请输入显示标题', trigger: 'blur' }]"
                  style="width: 100%"
                >
                  <el-input v-model="item.label" style="width: 450px" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item
                  label="显示图标"
                  :prop="'data.'+index+'.tb'"
                  :rules="[{ required: true, message: '请选择显示图标', trigger: 'change' }]"
                >
                  <el-popover
                    placement="bottom-start"
                    width="450"
                    trigger="click"
                  >
                    <el-button slot="reference" size="small" style="padding: 0px;">
                      <el-image
                        v-if="item.tb"
                        :src="baseURL+item.tb.substring(1,item.tb.length)"
                        style="height: 32px;width: 32px;"
                      />
                      <i v-else style="font-size: 20px;height: 32px;width: 32px;line-height: 32px" class="el-icon-plus" />
                    </el-button>
                    <abbreviation-image-select ref="iconSelect" v-model="item.tb" />
                  </el-popover>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item
                  label="数据来源"
                  :prop="'data.'+index+'.sjly'"
                  style="width: 100%"
                >
                  <el-popover
                    placement="bottom-start"
                    width="450"
                    trigger="click"
                  >
                    <notice-data ref="tempSelect" v-model="item.sjly" />
                    <el-input slot="reference" v-model="item.sjly.name" style="width: 450px" placeholder="点击选择模版" readonly />
                  </el-popover>
                </el-form-item>
              </el-col>
              <el-col v-if="item.sjly.jxx" :span="24">
                <el-form-item
                  label="数据项"
                  :prop="'data.'+index+'.number'"
                  :rules="[{ required: true, message: '请选择数据项', trigger: 'change' }]"
                >
                  <el-select v-model="item.number" clearable placeholder="请选择数据项" style="width: 450px">
                    <el-option
                      v-for="obj in item.sjly.jxx"
                      :key="obj"
                      :label="obj"
                      :value="obj"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="背景色A"
                  :prop="'data.'+index+'.backColorA'"
                  :rules="[{ required: true, message: '请选择背景色A', trigger: 'change' }]"
                  style="width: 100%"
                >
                  <el-color-picker v-model="item.backColorA" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="背景色B"
                  :prop="'data.'+index+'.backColorB'"
                  :rules="[{ required: true, message: '请选择背景色A', trigger: 'change' }]"
                  style="width: 100%"
                >
                  <el-color-picker v-model="item.backColorB" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="预览">
                  <div
                    :style="{ width: '120px',height: '60px',boxShadow: '0 2px 7px 0px '+ item.backColorA,
                              background: 'linear-gradient(0deg,'+item.backColorA+','+item.backColorB+')'}"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="显示标题大小"
                  :prop="'data.'+index+'.titleFontSize'"
                  :rules="[{ required: true, message: '显示标题大小', trigger: 'blur' }]"
                  style="width: 100%"
                >
                  <el-input-number v-model="item.titleFontSize" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="字体颜色"
                  :prop="'data.'+index+'.titleColor'"
                  :rules="[{ required: true, message: '请选择字体颜色', trigger: 'change' }]"
                  style="width: 100%"
                >
                  <el-color-picker v-model="item.titleColor" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="统计数大小"
                  :prop="'data.'+index+'.fontSize'"
                  :rules="[{ required: true, message: '统计数大小', trigger: 'blur' }]"
                  style="width: 100%"
                >
                  <el-input-number v-model="item.fontSize" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="字体颜色"
                  :prop="'data.'+index+'.fontColor'"
                  :rules="[{ required: true, message: '请选择字体颜色', trigger: 'change' }]"
                  style="width: 100%"
                >
                  <el-color-picker v-model="item.fontColor" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否打开详情" :prop="'data.'+index+'.isOpenMore'" :rules="[{ required: true, message: '请选择是否打开详情', trigger: 'change' }]">
                  <el-radio-group v-model="item.isOpenMore">
                    <el-radio
                      v-for="item in status"
                      :key="item.id"
                      :label="item.value"
                    >{{ item.label }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col v-if="item.isOpenMore === true " :span="12">
                <el-form-item label="打开方式" :prop="'data.'+index+'.openType'" :rules="[{ required: true, message: '请选择打开方式', trigger: 'change' }]">
                  <el-radio-group v-model="item.openType">
                    <el-radio
                      v-for="item in openType"
                      :key="item.id"
                      :label="item.value"
                    >{{ item.label }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col v-if="item.isOpenMore === true && item.openType === 1" :span="12">
                <el-form-item
                  label="关联模版"
                  :prop="'data.'+index+'.temp'"
                  :rules="[{ required: true, message: '请选择关联模版', trigger: 'change' }]"
                >
                  <el-select v-model="item.temp" clearable filterable placeholder="请选择关联模版">
                    <el-option
                      v-for="obj in tempList"
                      :key="obj.id"
                      :label="obj.name"
                      :value="obj.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-if="item.isOpenMore === true && item.openType === 2" :span="12">
                <el-form-item
                  label="打开地址"
                  :prop="'data.'+index+'.openUrl'"
                  :rules="[{ required: true, message: '请输入打开地址', trigger: 'blur' }]"
                  style="width: 100%"
                >
                  <el-input v-model="item.openUrl" clearable placeholder="请输入打开地址" />
                </el-form-item>
              </el-col>
              <el-col :span="4" style="text-align: center">
                <el-button type="text" icon="el-icon-delete" size="mini" @click="toDelete(item)">删除</el-button>
              </el-col>
            </el-row>
          </el-col>
        </draggable>
      </el-row>
    </el-form>
    <div class="custom-button">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </div>
</template>
<script>
import { queryAllByFl, updateObject } from '@/api/portal/manager/columnTemp';
import draggable from 'vuedraggable';
import NoticeData from '@/components/NoticeData';
import { querySjlx } from '@/api/portal/manager/setting';
import AbbreviationImageSelect from '@/components/AbbreviationImageSelect';
export default {
  name: 'Portal4PcStatisticsV1',
  components: { draggable, NoticeData, AbbreviationImageSelect },
  props: {
    data: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  data() {
    return {
      loading: false,
      dataTypes: [],
      status: [
        { id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }
      ],
      jxx: [],
      tempList: [],
      openType: [
        { id: 0, value: 1, label: '指定模版' },
        { id: 1, value: 2, label: '固定地址' }
      ],
      baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
      form: {
        rows: 1,
        data: []
      }
    };
  },
  watch: {
    data: {
      handler(newName, oldName) {
        if (undefined !== newName && newName.sjlx !== null) {
          if (undefined !== newName.pzx && newName.pzx !== '' && newName.pzx !== null) {
            this.form = JSON.parse(newName.pzx);
          }
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    // 解决火狐拖拽搜索的问题
    document.body.ondrop = function(event) {
      event.preventDefault();
      event.stopPropagation();
    };
    this.initTemp();
  },
  methods: {
    doAdd() {
      const item = { label: '', sjly: {}, temp: null, px: 0, backColorA: '', isOpenMore: false, openType: null, backColorB: '', titleFontSize: 16, fonSize: 18, titleColor: '', tb: '', openUrl: '' };
      this.form.data.push(item);
    },
    toDelete(item) {
      const index = this.form.data.indexOf(item);
      if (index !== -1) {
        this.form.data.splice(index, 1);
      }
    },
    clone() {
      this.form.data = this.form.data.map((item, index) => {
        item.px = index + 1;
        return item;
      });
    },
    initDataType() {
      querySjlx().then(res => {
        const { data } = res;
        this.dataTypes = data;
      });
    },
    initTemp() {
      queryAllByFl({ mbdl: 'TabsTemplate', pt: 'app' }).then(res => {
        const { data } = res;
        this.tempList = data.data;
      }).catch(() => {
        this.tempList = [];
      });
    },
    dataChange() {
      if (this.form.sjlx !== null && this.form.sjlx !== '') {
        if (this.form.sjlx === 1) {
          this.initDataSource();
        } else if (this.form.sjlx === 2) {
          this.initSystemData();
        }
      } else {
        this.form.sjx = null;
      }
    },
    save() {
      this.loading = true;
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.data.pzx = JSON.stringify(this.form);
          updateObject(this.data).then(res => {
            this.close();
          }).catch(() => {
            this.loading = false;
          });
        } else {
          this.loading = false;
        }
      });
    },
    close() {
      this.loading = false;
      Object.assign(this.$data.form, this.$options.data.call(this).form);
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    }
  }
};
</script>
<style scoped>
.setting-body {
  width: 100%;
}

.custom-tips {
  width: 100%;
  min-height: 40px;
  line-height: 40px;
  text-align: left;
  padding: 8px 16px;
  background-color: #ecf8ff;
  border-radius: 4px;
  border-left: 5px solid #50bfff;
}
.button-add {
  width: 50px;
  height: 40px;
  float: right;
  text-align: center;
  line-height: 40px;
  margin-right: 20px;
}
.custom-button {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: right;
}
</style>
